;(function($){
    $.fn.timesRoom_room_new = function(opts){
        var el = this;
        var opt = {
            xNum : 6,
            yNum : 5,
            currP : 1,
            totalP : 1,
            psize : 25,
            currentPageId : "currentPage",
            totalPageId : "totalPage",
            btnPageupId : "btnPageup",
            btnPagedownId : "btnPagedown",
            roomMsgEntity : null,
            roomMsgId : "roomMsg",
            dataBack : function(){},
            closeBack : function(){},
            statBack : function(){},//状态返回值
        }
        var param = $.extend(opt,opts);
        var app = new App(el,param);
    }

    var App = function(el,param){
        this.el = el;
        this.param = param;
        this.setSize();
        this.initHtml();
    }

    App.prototype = {
        bindEvent : function(){
            var el = this.el;
            var param = this.param;
            var t = this;

        },
        initHtml : function(){
            
            var el = this.el;
            var param = this.param;
            var t = this;
            this.getDs(function(ds){
                var html = "";
                var statArr = ds[0].stat||null;
                param.statBack(statArr);
                var total = param.xNum*param.yNum;
                if(ds.length>total){
                    param.ew = param.ew - (16/param.xNum);
                }
                for(var i=0;i<ds.length;i++){
                    //计算宽度
                    var width = param.ew;
                    if((i+1)%param.xNum==0&&ds.length<=total){
                        width = el.width()-((param.xNum-1)*param.ew)-0.5;
                    }
                    var bodyHtml = t.initRmBody(ds[i].info_remark,ds[i].info_guest,ds[i].info_timer);
                    html += '<div class="rm-box" style="height:'+param.eh+'px;width:'+width+'px;">'+//style="height:'+param.eh+'px;width:'+param.ew+'px;"
                                // '<div class="rm-header" style="background:'+ds[i].bgcolor+' !important;">'+
                                //     (ds[i].room_c||"")+
                                // '</div>'+
                                t.initRmHeader(ds[i].info_room)+
                                '<div class="rm-body" data-code="'+ds[i].code+'" style="height:'+(param.eh-22)+'px;width:'+(width-2)+'px;">'+//style="height:'+(param.eh-22)+'px;width:'+(param.ew-2)+'px;"
                                    '<div class="json hide">'+
                                        JSON.stringify(ds[i])+
                                    '</div>'+
                                    bodyHtml+
                                '</div>'+
                            '</div>';
                }
              
                el.html(html);
                el.find(".rm-box").unbind("click").click(function(){
                    el.find(".rm-body.active").removeClass("active");
                    $(this).find(".rm-body").addClass("active");
                    var room = $(this).find(".rm-header").data("room");
                    t.getRoomInfo(room);
                })
                el.find(".rm-box").eq(0).trigger("click");
            })
        },
        setSize : function(){
            var el = this.el;
            var param = this.param;
            var t = this;
            
            var width = el.width() //- 18;
            var height = el.height();
            var ew = parseInt(width/param.xNum);
            var eh = parseFloat(height/param.yNum);
            param.ew = ew;
            // if(param.ew<230){
            //     param.ew = 230;
            // }
            param.eh = eh-4;
        },
        getDs : function(back){
            var el = this.el;
            var param = this.param;
            var t = this;
            
            var arg1 = {"where":{"bmh":$.HSAPI_HEADERS.bmh,rmtype:param.rmtype},"session":{"hotelid":$.HSAPI_HEADERS.hotelId,"bmh":$.HSAPI_HEADERS.bmh}}
            var size = $("#statContainer").find(".color-item.active").size();
            if(size==1){
                arg1.where.status = $("#statContainer").find(".color-item.active").data("status");
            }
            $.HSAPI_CALLPROCEDURE2(arg1,"SPARetrieveTableView",{},function(data){
                if(data.RETURN==1){
                    back(data.DATA)
                }else{
                    $.HSAPI_ALERT(data.MSG)
                }
            },function(data){
                console.log(data)
            },"list");
        },
        initRmHeader : function(info_room){
            var el = this.el;
            var param = this.param;
            var t = this;
            var html = "";
            if(info_room!="[]"){
                var ds = $.parseJSON(info_room);
                $(ds).each(function(k,v){
                    html += '<div class="rm-header" data-room="'+v.value+'" style="background:'+v.bgcolor+' !important;color:'+v.tcolor+' !important;">'+
                                v.value+
                            '</div>';
                })
            }
            return html;
        },
        initRmBody : function(info_remark,info_guest,info_timer){
            var el = this.el;
            var param = this.param;
            var t = this;
            var html = "";

            if(info_remark!="[]"){
                var ds = $.parseJSON(info_remark);
                $(ds).each(function(k,v){
                    if(v.value){
                        var bgcolor = t.colorRgb(v.bgcolor);
                        html += '<div class="rm-fc-grey" style="background:'+bgcolor+' !important;color:'+v.tcolor+';">'+
                                    '<i class="ace-icon glyphicon '+v.icon+'"></i>'+
                                    // '<span class="rm-time">'+v.name+'</span> '+
                                    '<span class="rm-title" title="'+v.value+'">'+v.value+'</span>'+
                                '</div>'
                    }
                })
            }

            if(info_guest!="[]"){
                var ds = $.parseJSON(info_guest);
                $(ds).each(function(k,v){
                    html += '<div class="rm-fc-grey" style="background:'+v.bgcolor+' !important;color:'+v.tcolor+';">'+
                                '<i class="ace-icon glyphicon '+v.icon+'"></i>'+
                                // '<span class="rm-time">'+v.name+'</span> '+
                                '<span class="rm-title" title="'+v.value+'">'+v.value+'</span>'+
                            '</div>'
                })
            }

            if(info_timer!="[]"){
                var ds = $.parseJSON(info_timer);
                $(ds).each(function(k,v){
                    html += '<div class="rm-fc-grey" style="background:'+v.bgcolor+' !important;color:'+v.tcolor+';">'+
                                '<i class="ace-icon glyphicon '+v.icon+'"></i>'+
                                // '<span class="rm-time">'+v.name+'</span> '+
                                '<span class="rm-title" title="'+v.value+'">'+v.value+'</span>'+
                            '</div>'
                })
            }

            return html;

        },
        getRoomInfo : function(room){
            var el = this.el;
            var param = this.param;
            var t = this;

            if(param.roomMsgEntity==null){
                if(!$.isFunction($().retrieve_room_msg)){
                    $.ajaxSetup({async: false,cache: false });
                    $.getScript("js/timesRoom/jquery.service.retrieve_room_msg.js");
                    $.ajaxSetup({async:true});
                }
                $("#"+param.roomMsgId).retrieve_room_msg({
                    room : room,
                    closeBack:function(){
                    },
                    dataBack:function(){
                    },
                    entityBack : function(entity){
                        param.roomMsgEntity = entity;
                    }
                });
            }else{
                param.roomMsgEntity.getDs(room);
            }
        },
        colorRgb : function(tttt){
            // 16进制颜色值的正则
            var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
            // 把颜色值变成小写
            var color = tttt.toLowerCase();
            if (reg.test(color)) {
                // 如果只有三位的值，需变成六位，如：#fff => #ffffff
                if (color.length === 4) {
                var colorNew = "#";
                for (var i = 1; i < 4; i += 1) {
                    colorNew += color.slice(i, i + 1).concat(color.slice(i, i + 1));
                }
                color = colorNew;
                }
                // 处理六位的颜色值，转为RGB
                var colorChange = [];
                for (var i = 1; i < 7; i += 2) {
                colorChange.push(parseInt("0x" + color.slice(i, i + 2)));
                }
                return "RGB(" + colorChange.join(",") + ",0)";
            } else {
                return color;
            }
        }
    }

})(jQuery);